
/*
 *
 * Copyright (C) 2008 Mattias Blomqvist, patr-blo at dsv dot su dot se
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

using System.Collections.Generic;
using System;
using FlowLib.Containers;
using ClientExample.Containers;

namespace ClientExample.Comparers
{
    public class UserInfoComparer : IComparer<UserInfo>
    {
        [Flags]
        public enum Columns
        {
            ID,                 /* STRING */
            StoreId,            /* STRING */
            DisplayName,        /* STRING */
            Email,              /* STRING */
            Description,        /* STRING */
            Connection,         /* STRING */
            Account,            /* INT: Account level. Usualy just OP and normal user */
            Share,              /* STRING TO INT*/
            IP,                 /* STRING */
            Tag,                /* STRING */
            Tag_Mode,           /* ENUM: Passive, Active.. */
            Tag_HubCount,       /* INT: NORMAL, REG, OP */
            Tag_Slots,          /* INT */
            Tag_Version         /* STRING */
        }

        public Columns SortOnColumn
        {
            get;
            set;
        }

        public bool Reverse
        {
            get;
            set;
        }

        public UserInfoComparer()
        {
            SortOnColumn = Columns.ID;
        }

        public UserInfoComparer(Columns sortOn)
        {
            SortOnColumn = sortOn;
            Reverse = false;
        }

        public int Compare(UserInfo x, UserInfo y)
        {
            if (Reverse)
            {
                UserInfo z = x;
                x = y;
                y = z;
            }
            int compValue = -1;
            switch (SortOnColumn)
            {
                case Columns.ID:
                    compValue = x.ID.CompareTo(y.ID);
                    break;
                case Columns.StoreId:
                    compValue = x.StoreID.CompareTo(y.StoreID);
                    break;
                case Columns.DisplayName:
                    compValue = x.DisplayName.CompareTo(y.DisplayName);
                    break;
                case Columns.Email:
                    compValue = x.Email.CompareTo(y.Email);
                    break;
                case Columns.Description:
                    compValue = x.Description.CompareTo(y.Description);
                    break;
                case Columns.Connection:
                    compValue = x.Connection.CompareTo(y.Connection);
                    break;
                case Columns.Account:
                    compValue = x.Account.CompareTo(y.Account);
                    break;
                case Columns.Share:
                    long xLong = 0;
                    long yLong = 0;
                    long.TryParse(x.Share, out xLong);
                    long.TryParse(y.Share, out yLong);
                    compValue = xLong.CompareTo(yLong);
                    break;
                //case Columns.IP:
                //    compValue = x.ip .CompareTo(y.ID);
                //    break;
                case Columns.Tag:
                    compValue = x.TagInfo.Tag.CompareTo(y.TagInfo.Tag);
                    break;
                case Columns.Tag_Mode:
                    compValue = x.TagInfo.Mode.CompareTo(y.TagInfo.Mode);
                    break;
                case Columns.Tag_HubCount:
                    int xInt = x.TagInfo.Normal + x.TagInfo.Regged + x.TagInfo.OP;
                    int yInt = y.TagInfo.Normal + y.TagInfo.Regged + y.TagInfo.OP;
                    compValue = xInt.CompareTo(yInt);
                    break;
                case Columns.Tag_Slots:
                    compValue = x.TagInfo.Slots.CompareTo(y.TagInfo.Slots);
                    break;
                case Columns.Tag_Version:
                    compValue = x.TagInfo.Version.CompareTo(y.TagInfo.Version);
                    break;
            }
            if (SortOnColumn != Columns.ID && SortOnColumn != Columns.StoreId && compValue == 0)
                compValue = x.ID.CompareTo(y.ID);
            return compValue;
        }
    }
}
